package com.wanlink.mall.base;

import android.os.Bundle;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.annotations.Nullable;

/**
 * user：chenqm
 * desc：BaseFragment
 */

public abstract class BaseFragment<V, T extends BasePresenter<V>> extends BFragment {

    protected T mPresenter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //判断是否使用MVP模式
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);//因为之后所有的子类都要实现对应的View接口
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
    //用于创建Presenter和判断是否使用MVP模式(由子类实现)
    protected abstract T createPresenter();

}
